package org.example.springcloudgateway;
import org.example.model.RemoteConfig;
import org.example.springcloudgateway.filter.myCustomFilter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DynamicRouteConfig extends RemoteConfig {

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        System.out.println("Custom RouteLocator"+this.AccessSecret);
      myCustomFilter s  =   new myCustomFilter();
        return builder.routes()
                .route("user-service", r -> r.path("/user-service/**").filters(f -> f.stripPrefix(1).filter(s.GetMyCustomFilter(this.AccessSecret)))
                        .uri("lb://user-service")  // 使用负载均衡路由
                       )
                .route("admin-service", r -> r.path("/admin-service/**").filters(f -> f.stripPrefix(1).filter(s.GetMyCustomFilter(this.AccessSecret)))
                        .uri("lb://admin-service"))
                .build();
    }
}
